APOD Pseudocode

html download module

  1. Connect to NASA APOD site
  2. Download apod html
  3. return raw html

extract image module

  1. extract image url
  2. extract image name
  3. download hi-res image

extract title module

  1. extract title
  2. remove html tags

extract explanation module

  1. extract explanation text
  2. remove html tags
  3. del all up to 1st Explanation
  4. remove html, line breaks and unneeded spaces:

create annotated image


In [86]:
#!/home/sean/anaconda3/bin/python

import requests
import json
import textwrap
import os
from PIL import Image, ImageDraw, ImageFont
from pathlib import Path
from configparser import ConfigParser

home = str(Path.home())

# Read from config file
parser = ConfigParser()
# configFilePath = home + '.apod/apod.settings'
configFilePath = 'apod.settings'
parser.read(configFilePath)
scrn_width = int(parser.get('apod_config', 'Screen_Size').split(',')[0])
scrn_length = int(parser.get('apod_config', 'Screen_Size').split(',')[1])
num_chars = int(parser.get('apod_config', 'Num_Characters'))
api_key = parser.get('apod_config', 'API_Key')

url = 'https://api.nasa.gov/planetary/apod'
data = json.loads(requests.get(url + '?api_key=' + api_key).text)
image = Image.open(requests.get(data['hdurl'], stream=True).raw).resize(
    (scrn_width, scrn_length), resample=0)
draw = ImageDraw.Draw(image)
exp_font = ImageFont.truetype("arial.ttf", 14, encoding="unic")
title_font = ImageFont.truetype("arial.ttf", 24, encoding="unic")
lines = textwrap.wrap(data['explanation'], width=num_chars)
y_text = (scrn_length / 6) * 5

draw.text((20, y_text - 30), data['title'], fill='white', font=title_font)

for line in lines:
    width, height = exp_font.getsize(line)
    draw.text((20, y_text), line, font=exp_font, fill='white')
    y_text += height

image.save(home + "/.apod/apod.png", 'PNG')
os.system("/usr/bin/gsettings set org.gnome.desktop.background picture-uri file://" +
          home + "/.apod/apod.png")


Out[86]:
0